﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Station.Inspect.SuaModel
{
    public class ModelBase
    {
        protected int minID = -1;
        protected int maxID = -1;

        protected bool TryParseName(string fileName)
        {
			int nPos = fileName.LastIndexOf('\\');
			string name = fileName.Substring(nPos + 1);
			nPos = name.IndexOf("_");
			if (nPos == -1)
				return false;
			name = name.Substring(0, nPos);
			nPos = name.IndexOf("-");
			if (nPos == -1)
			{
				minID = int.Parse(name);
				maxID = minID;
			}
			else
			{
				minID = int.Parse(name.Substring(0, nPos));
				maxID = int.Parse(name.Substring(nPos + 1));
			}
			return true;
		}
        public bool InRange(int libID)
        {
            return minID <= libID && libID <= maxID;
        }

		protected static BitmapInfo GetImagePixel(Bitmap Source)
		{
			//byte[] result;
			int step;
			int iWidth = Source.Width;
			int iHeight = Source.Height;

			Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
			System.Drawing.Imaging.BitmapData bmpData = Source.LockBits(rect,
				System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat);
			IntPtr iPtr = bmpData.Scan0;
			//int iBytes = iWidth * iHeight * 3;
			step = bmpData.Stride / iWidth;
			int iStride = bmpData.Stride;
			//int iBytes = iWidth * iHeight * step;
			int iBytes = Math.Abs(bmpData.Stride) * iHeight;
			byte[] PixelValues = new byte[iBytes];
			System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes);
			Source.UnlockBits(bmpData);
			// result = PixelValues;
			BitmapInfo bi = new BitmapInfo { Result = PixelValues, Channels = step, Stride = iStride, width = iWidth, height = iHeight };
			return bi;
		}
	}
}
